package model;

public abstract class Act_Deplacement extends Action{

	static protected int maxX = Territoire.getMaxX(); 
	static protected int maxY = Territoire.getMaxY();
	
	
	public Act_Deplacement(Individu _indiv) {
		super(_indiv);		
	}
	
	@Override
	protected abstract void updatePriority();

	@Override
	protected abstract void doAction();	
		
	protected void moveLeft(){
		if(indiv.getX()>0){
			ExitCell();
			indiv.setX(indiv.getX()-MonTimer.getTimeUnit());
			
			indiv.getCellPos().getLeft().indivEntering(indiv);
			Territoire.updateCellStatut(indiv.getCellPos());			
			indiv.setCellPos(indiv.getCellPos().getLeft());
			Territoire.updateCellStatut(indiv.getCellPos());
		}
	}
	protected void moveRight(){
		if(indiv.getX()<maxX){
			ExitCell();
			indiv.setX(indiv.getX()+MonTimer.getTimeUnit());

			indiv.getCellPos().getRight().indivEntering(indiv);
			Territoire.updateCellStatut(indiv.getCellPos());	
			indiv.setCellPos(indiv.getCellPos().getRight());
			Territoire.updateCellStatut(indiv.getCellPos());	
			
		}
	}	
	protected void moveUp(){
		if(indiv.getY()>0){
			ExitCell();
			indiv.setY(indiv.getY()-MonTimer.getTimeUnit());

			indiv.getCellPos().getUp().indivEntering(indiv);
			Territoire.updateCellStatut(indiv.getCellPos());	
			indiv.setCellPos(indiv.getCellPos().getUp());
			Territoire.updateCellStatut(indiv.getCellPos());	
			
		}
	}	
	protected void moveDown(){
		if(indiv.getY()<maxY){
			ExitCell();
			indiv.setY(indiv.getY()+MonTimer.getTimeUnit());

			indiv.getCellPos().getDown().indivEntering(indiv);
			Territoire.updateCellStatut(indiv.getCellPos());	
			indiv.setCellPos(indiv.getCellPos().getDown());
			Territoire.updateCellStatut(indiv.getCellPos());	
			
		}
	}
	
	protected void moveTo(int _toX, int _toY){
		
		int hX = indiv.getX();
		int hY = indiv.getY();
		
		if(Math.abs((hX-_toX))>Math.abs((hY-_toY))){
			if(hX<_toX){
				moveRight();		
			}else if(hX>_toX){
				moveLeft();	
			}
		}else{
			if(hY<_toY){
				moveDown();	
			}else if(hY>_toY){
				moveUp();
			}
		}		
	}
	
	protected void moveFrom(int _toX, int _toY){
		int hX = indiv.getX();
		int hY = indiv.getY();
		
		if(Math.abs((hX-_toX))<Math.abs((hY-_toY))){
			if(hX>=_toX){
				moveRight();
			}else{
				moveLeft();	
			}
		}else{
			if(hY>=_toY){
				moveDown();	
			}else{
				moveUp();
			}
		}
	}


	protected void radomMove(){
		int rand = actionRandomSeed.nextInt(4);
		switch (rand){
			case 0 : moveLeft();
			case 1 : moveUp();
			case 2 : moveDown();
			case 3 : moveRight();
		}
	}
	
	private void ExitCell(){
		indiv.getCellPos().indivExit(indiv);
	}
}
